#include <gtk/gtk.h>

#include "tsapp.h"
#include "tsappwin.h"

struct _TsApp
{
  GtkApplication parent;
};

G_DEFINE_TYPE(TsApp, ts_app, GTK_TYPE_APPLICATION);

static void
ts_app_init (TsApp *ts_app)
{
}


static void
ts_app_startup (GApplication *ts_app)
{
  G_APPLICATION_CLASS (ts_app_parent_class)->startup (ts_app);
}

static void
ts_app_activate (GApplication *ts_app)
{
  TsAppWindow *win;

  win = ts_app_window_new (TS_APP (ts_app));
  gtk_window_present (GTK_WINDOW (win));
}

static void
ts_app_open (GApplication  *ts_app,
                  GFile        **files,
                  gint           n_files,
                  const gchar   *hint)
{
  GList *windows;
  TsAppWindow *win;
  int i;

  windows = gtk_application_get_windows (GTK_APPLICATION (ts_app));
  if (windows)
    win = TS_APP_WINDOW (windows->data);
  else
    win = ts_app_window_new (TS_APP (ts_app));

  for (i = 0; i < n_files; i++)
    ts_app_window_open (win, files[i]);

  gtk_window_present (GTK_WINDOW (win));
}

static void
ts_app_class_init (TsAppClass *class)
{
  G_APPLICATION_CLASS (class)->startup = ts_app_startup;
  G_APPLICATION_CLASS (class)->activate = ts_app_activate;
  G_APPLICATION_CLASS (class)->open = ts_app_open;
}

TsApp *
ts_app_new (void)
{
  return g_object_new (TS_APP_TYPE,
                       "application-id", "org.gtk.ts_app",
                       "flags", G_APPLICATION_HANDLES_OPEN,
                       NULL);
}
